package com.graduation.smartparkingplatform.ServiceImpl;

import com.graduation.smartparkingplatform.Dao.DiscernMapper;
import com.graduation.smartparkingplatform.Pojo.WebSocketDistinguish;
import com.graduation.smartparkingplatform.Service.DiscernService;
import com.graduation.smartparkingplatform.WebSocket.WebSocket;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Calendar;

@Service
public class DiscernServiceImpl implements DiscernService {
  @Autowired DiscernMapper discernMapper;

  // 识别记录（进）
  @Override
  public Boolean addRecordIn(String carPlate, String fileUrl, Integer confidence, String requestId)
      throws IOException {
    String type = "进";
    // 获取当前时间
    Calendar calendar = Calendar.getInstance();
    // 定义时间格式化
    SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    String nowTime = formatter.format(calendar.getTime());
    WebSocketDistinguish webSocketDistinguish = new WebSocketDistinguish(type, carPlate, nowTime);
    WebSocket.sendInfo(webSocketDistinguish.toString());
    return discernMapper.addRecordIn(type, carPlate, fileUrl, confidence, requestId, nowTime);
  }

  // 识别记录（出）
  @Override
  public Boolean addRecordOut(String carPlate, String fileUrl, Integer confidence, String requestId)
      throws IOException {
    String type = "出";
    // 获取当前时间
    Calendar calendar = Calendar.getInstance();
    // 定义时间格式化
    SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    String nowTime = formatter.format(calendar.getTime());
    WebSocketDistinguish webSocketDistinguish = new WebSocketDistinguish(type, carPlate, nowTime);
    WebSocket.sendInfo(webSocketDistinguish.toString());
    return discernMapper.addRecordIn(type, carPlate, fileUrl, confidence, requestId, nowTime);
  }
}
